<?php

class RankingsController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $auth = Zend_Auth::getInstance();
       	if ($auth->hasIdentity()) 
        {
            $user_id = $auth->getIdentity()->id; 
        }
        else 
        {
        	$this->_redirect('authentication/login');
        }
        $nr_of_user_per_page = 15;
    	$rankingsPage = new Application_Model_RankingsPage();
    	$rankingList = $rankingsPage->getRankingsPageList();
		
    	$power = $this->view->playerStats['power'];
    	$playerRank = $rankingsPage->getPlayerRank($power,$user_id);
    	
    	$currentPage = ceil($playerRank/$nr_of_user_per_page);
    	
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($rankingList));
        $paginator->setItemCountPerPage($nr_of_user_per_page)
        		  ->setCurrentPageNumber($this->_getParam('page',$currentPage));
        $this->view->paginator = $paginator;
        $this->view->userId = $user_id;
        $page = $paginator->getCurrentPageNumber();
        if($page == 1)
        	$this->view->row_index = 0;
       	else
       		$this->view->row_index = $nr_of_user_per_page * ($page-1);
       		
       	$gotoPageForm = new Application_Form_Pagination();
       	$gotoPageForm->pageNumber->setValue($page);

       	$this->view->gotoPage = $gotoPageForm;
       	
       	$currUrl = $this->getRequest()->getRequestUri();
    	if($this->getRequest()->isPost())
	    {
	    	if($gotoPageForm->isValid($_POST))
	    	{
	    		$gotoPageNumber = $gotoPageForm->getValue('pageNumber');
	    		if($gotoPageNumber > $paginator->count() )
	    		{
	    			$gotoPageNumber = $paginator->count();
	    		}
    			$paginator->setCurrentPageNumber($this->_getParam('page',$gotoPageNumber));

    			$redirector = $this->_helper->getHelper('Redirector');
	    		$redirector->gotoSimple('index',
                                       'rankings',
                                       null,
                                       array('page' => $gotoPageNumber
                                             ));		}
	    }
        
    }
    
   


}

